// --- Copyright (c) 2006-2008 Stefan Kyntchev ---
// This software is written and copyrighted by Stefan Kyntchev 
// and BeyondPod Team members. All rights are reserved.
// Author contact: support@beyondpod.mobi
// ------------------------------------------------------------------------
// This file is part of BeyondPod RSS Feed Reader and Podcast manager
// (www.codeplex.com/BeyondPod) 
// BeyondPod is free software: you can redistribute it and/or modify 
// it under the terms of the GNU General Public License as published by 
// the Free Software Foundation, either version 3 of the License, or 
// (at your option) any later version. 
//  
// BeyondPod is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
// GNU General Public License for more details. 
//  
// You should have received a copy of the GNU General Public License
// along with BeyondPod. If not, see <http://www.gnu.org/licenses/>
// ------------------------------------------------------------------------
// COPYRIGHT NOTICE: This copyright notice may NOT be removed, obscured or modified 
// without written consent from the author.
// --- End copyright notice --- 


using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using svs.Mobile.BeyondMedia.FeedCore;
using svs.Mobile.BeyondMedia.FeedCore.RSS;

namespace svs.Mobile.BeyondMedia.Objects
{
	internal static class MenuManager
	{
		#region WellKnownMenuItems enum

		public enum WellKnownMenuItems
		{
			Feed_ManageFeeds,
			FeedContent_ViewPodcasts,
			FeedContent_GoBack,
			FeedContent_Update,
			Feed_ManageFeeds_DeleteFeed,
			Feed_ManageFeeds_EditFeed,
			Feed_UpdateStop,
			Feed_UpdateStart,
			Feed_UpdateCurrentCategory,
			Feed_Open,
			Feed_ViewPlayer
		}

		#endregion

		private static readonly List<MenuItem> m_FeedsRightMenus = new List<MenuItem>();

		private static MainMenu m_BrowserMenu;
		private static MainMenu m_CategoriesMenu;
		private static MainMenu m_FeedContentMenu;

		private static MainMenu m_FeedsMenu;
		private static MainMenu m_PlayerMenu, m_TracksMenu;
		public static Dictionary<WellKnownMenuItems, MenuItem> WellKnownMenus = new Dictionary<WellKnownMenuItems, MenuItem>();

		static MenuManager()
		{
			BuildFeedContentMenu();
			BuildBrowserMenu();
			BuildFeedsMenu();
			BuildPlayerMenu();
			BuildTracksMenu();
			BuildCategoriesMenu();
		}

		/// <summary>
		/// Builds the menu.
		/// </summary>
		/// <param name="i_view">The i_view.</param>
		/// <returns></returns>
		public static MainMenu GetMenu(Helpers.ViewNames i_view)
		{
			switch (i_view)
			{
				case Helpers.ViewNames.Feeds:
					return m_FeedsMenu;
				case Helpers.ViewNames.Player:
					return m_PlayerMenu;
				case Helpers.ViewNames.Tracks:
					return m_TracksMenu;
				case Helpers.ViewNames.FeedContent:
					return m_FeedContentMenu;
				case Helpers.ViewNames.WebBrowser:
					return m_BrowserMenu;
				case Helpers.ViewNames.Categories:
					return m_CategoriesMenu;
			}

			return null;
		}

		/// <summary>
		/// Builds the tracks menu.
		/// </summary>
		/// <returns></returns>
		private static void BuildTracksMenu()
		{
			m_TracksMenu = new MainMenu();

			var ViewFeedContent = new MenuItem();
			var StartStopDownload = new MenuItem();

			var ViewFeeds = new MenuItem();
			var Menu = new MenuItem();
			var DeleteSelected = new MenuItem();
			var DeletePlayed = new MenuItem();
			var TogglePlayed = new MenuItem();
			var ToggleViewAll = new MenuItem();
			var ToggleGroupByFeed = new MenuItem();
			var exit = new MenuItem();

			// 
			// mainMenu1
			// 
			m_TracksMenu.MenuItems.Add(ViewFeeds);
			m_TracksMenu.MenuItems.Add(Menu);
			// 
			// GoBack
			// 
			ViewFeeds.Text = "Feeds";
			ViewFeeds.Click += GoBack_Click;

			var ViewPlayer = new MenuItem();
			ViewPlayer.Text = "Player";
			ViewPlayer.Click += delegate { CommandManager.CmdGoToView(Helpers.ViewNames.Player); };

			// 
			// Menu
			// 
			Menu.MenuItems.Add(ViewPlayer);
			Menu.MenuItems.Add(ViewFeedContent);
			Menu.MenuItems.Add(ToggleViewAll);
			Menu.MenuItems.Add(ToggleGroupByFeed);
			Menu.MenuItems.Add(StartStopDownload);
			Menu.MenuItems.Add(DeleteSelected);
			Menu.MenuItems.Add(DeletePlayed);
			Menu.MenuItems.Add(TogglePlayed);

			Menu.MenuItems.Add(exit);
			Menu.Text = "Menu";

			DeleteSelected.Text = "Delete";
			DeleteSelected.Click += DeleteSelected_Click;

			DeletePlayed.Text = "Delete Played";
			DeletePlayed.Click += DeletePlayedInFeed_Click;

			TogglePlayed.Text = "Toggle Played";
			TogglePlayed.Click += TogglePlayed_Click;

			ToggleViewAll.Text = "Show Category Podcasts";
			ToggleViewAll.Click += ToggleViewAll_Click;

			ToggleGroupByFeed.Text = "Group By Feed";
			ToggleGroupByFeed.Click += delegate { CommandManager.CmdToggleGroupTracksByFeed(); };

			// 
			// PlayerExit
			// 
			exit.Text = "Exit";
			exit.Click += Exit_Click;

			StartStopDownload.Text = "Stop Download";
			StartStopDownload.Click += StartStopDownload_Click;

			ViewFeedContent.Text = "View Content";
			ViewFeedContent.Click += ViewFeedContent_Click;

			Menu.Popup += delegate
			{
				ViewFeedContent.Enabled = ViewManager.Tracks.SelectedTrack != null && ViewManager.Tracks.SelectedTrack.ParentFeed.HasUrl;
				DeleteSelected.Enabled = TogglePlayed.Enabled = ViewManager.Tracks.SelectedTrack != null;
				ViewPlayer.Enabled = ViewManager.MediaPlayer.HasPlaylist;

				if (EnclosureDownloadManager.IsDownloading)
				{
					StartStopDownload.Text = "Stop Download";
					StartStopDownload.Enabled = true;
				}
				else
				{
					StartStopDownload.Text = "Resume Download";

					if (ViewManager.Tracks.SelectedTrack == null || ViewManager.Tracks.SelectedTrack.IsFullyDownloaded)
						StartStopDownload.Enabled = false;
					else
						StartStopDownload.Enabled = true;
				}

				ToggleViewAll.Text = ViewManager.Tracks.HasFeedFilter()
				                     	? string.Format(
				                     	  	"Show all in '{0}'",
				                     	  	Configuration.ActiveFeedCategory
				                     	  		.Name)
				                     	: "Show Current Feed only";

				ToggleGroupByFeed.Enabled = !ViewManager.Tracks.HasFeedFilter();
				ToggleGroupByFeed.Checked = Configuration.GroupByFeedIfShowingCategoryTracks;

			};
		}


		/// <summary>
		/// Builds the player menu.
		/// </summary>
		/// <returns></returns>
		private static void BuildPlayerMenu()
		{
			m_PlayerMenu = new MainMenu();

			var GoBack = new MenuItem();
			var Menu = new MenuItem();
			//var PlayerView = new MenuItem();
			//var PlayerViewSmall = new MenuItem();
			//var PlayerViewNormal = new MenuItem();
			var PlayerExit = new MenuItem();
			var ViewFeedContent = new MenuItem();
			var DeletePlayed = new MenuItem();
			var PlayPause = new MenuItem();
			var NextTrack = new MenuItem();
			var PrevTrack = new MenuItem();
			var SkipToEnd = new MenuItem();
			var ScreenOff = new MenuItem();
			var ViewTracks = new MenuItem();

			// 
			// mainMenu1
			// 
			m_PlayerMenu.MenuItems.Add(GoBack);
			m_PlayerMenu.MenuItems.Add(Menu);
			// 
			// GoBack
			// 
			GoBack.Text = "Feeds";
			GoBack.Click += GoBack_Click;
			// 
			// Menu
			// 
			Menu.MenuItems.Add(ViewTracks);
			Menu.MenuItems.Add(SkipToEnd);
			Menu.MenuItems.Add(PlayPause);
			Menu.MenuItems.Add(PrevTrack);
			Menu.MenuItems.Add(NextTrack);
			Menu.MenuItems.Add(ScreenOff);
			Menu.MenuItems.Add(ViewFeedContent);
			Menu.MenuItems.Add(PlayerExit);
			Menu.Text = "Menu";
			// 
			// PlayerView
			// 
			//PlayerView.MenuItems.Add(PlayerViewSmall);
			//PlayerView.MenuItems.Add(PlayerViewNormal);
			//PlayerView.MenuItems.Add(PlayerViewFullScreen);
			//PlayerView.Text = "View";
			// 
			// PlayerViewSmall
			// 
			//PlayerViewSmall.Checked = true;
			//PlayerViewSmall.Text = "Compact";
			//PlayerViewSmall.Click += PlayerViewSmall_Click;
			// 
			// playerViewNormal
			// 
			//PlayerViewNormal.Text = "Normal";
			//PlayerViewNormal.Click += PlayerViewNormal_Click;

			// 
			// PlayerViewFullScreen
			// 
			//PlayerViewFullScreen.Text = "Full Screen";
			//PlayerViewFullScreen.Click += PlayerViewFullScreen_Click;
			// 
			// PlayerExit
			// 
			PlayerExit.Text = "Exit";
			PlayerExit.Click += Exit_Click;
			// 
			// PlayerMapKeys
			// 
			ViewFeedContent.Text = "View Content";
			ViewFeedContent.Click += delegate
			                         	{
			                         		CommandManager.CmdGoToView(Helpers.ViewNames.FeedContent);
			                         	};

			SkipToEnd.Text = "Skip to the end";
			SkipToEnd.Click += delegate { CommandManager.CmdGoToTheEndOfTrack(); };

			ScreenOff.Text = "Turn Screen Off";
			ScreenOff.Click += delegate { DelayedCommandExecutor.ExecuteCommand(1500, CommandManager.CmdTurnBacklightOff); };

			DeletePlayed.Text = "Delete Played";
			DeletePlayed.Click += DeletePlayedInFeed_Click;

			PlayPause.Text = "Play/Pause";
			PlayPause.Click += PlayPause_Click;

			NextTrack.Text = "Play Next";
			NextTrack.Click += PlayNext_Clik;

			PrevTrack.Text = "Play Previous";
			PrevTrack.Click += PrevTrack_Click;

			ViewTracks.Text = "Podcasts";
			ViewTracks.Click += delegate
			                    	{
			                    		CommandManager.CmdGoToView(Helpers.ViewNames.Tracks);
			                    	};

			Menu.Popup += delegate
			              	{
			              		ViewFeedContent.Enabled = ViewManager.FeedContent.CurrentRssFeed != null;
			              	};
		}

		/// <summary>
		/// Builds the feeds menu.
		/// </summary>
		/// <returns></returns>
		private static void BuildFeedsMenu()
		{
			var OpenSelectedFeed = new MenuItem();
			var ExitApplication = new MenuItem();
			var Tools = new MenuItem();

			var DeleteAllPlayed = new MenuItem();
			var DeleteOldTracks = new MenuItem();
			var Refresh = new MenuItem();
			var Settings = new MenuItem();
			var AddFeed = new MenuItem();
			var DeleteFeed = new MenuItem();
			var FeedProperties = new MenuItem();
			var About = new MenuItem();
			var Contribute = new MenuItem();
			var ReportBug = new MenuItem();
			var Menu = new MenuItem();
			var ManageFeeds = new MenuItem();
			var StopUpdate = new MenuItem();
			var StartUpdate = new MenuItem();
			var UpdateAll = new MenuItem();
			var UpdateCategory = new MenuItem();
			var UpdateFeed = new MenuItem();
			var UpdateFailedFeeds = new MenuItem();
			var ShowCategories = new MenuItem();
			var ClearCache = new MenuItem();
			var CheckForUpdates = new MenuItem();

			var ImportOPML = new MenuItem();
			var ExportOPML = new MenuItem();
			var ShowUpdateReport = new MenuItem();
			var Help = new MenuItem();
			var ShowHelp = new MenuItem();
			var WhatIsNew = new MenuItem();

			// 
			// Play
			// 
			OpenSelectedFeed.Text = "Open";
			OpenSelectedFeed.Click += Open_Click;
			WellKnownMenus[WellKnownMenuItems.Feed_Open] = OpenSelectedFeed;
			// 
			// Exit
			// 
			ExitApplication.Text = "Exit";
			ExitApplication.Click += Exit_Click;

			Tools.Text = "Tools";

			DeleteAllPlayed.Text = "Delete All Played";
			DeleteAllPlayed.Click += DeleteAllPlayed_Click;

			DeleteOldTracks.Text = "Delete Old Podcasts";
			DeleteOldTracks.Click += DeleteOldTracks_Click;
			// 
			// Refresh
			// 
			Refresh.Text = "Refresh";
			Refresh.Click += Refresh_Click;

			FeedProperties.Text = "Edit Feed";
			WellKnownMenus[WellKnownMenuItems.Feed_ManageFeeds_EditFeed] = FeedProperties;
			FeedProperties.Click += FeedProperties_Click;


			WhatIsNew.Text = "What Is New...";
			WhatIsNew.Click += delegate { CommandManager.CmdShowWhatIsNewInCurrentCategory(); };

			Settings.Text = "Settings";
			Settings.Click += delegate { CommandManager.CmdShowSettingsForm(); };

			About.Text = "About";
			About.Click += About_Click;

			Contribute.Text = "Donate";
			Contribute.Click += delegate { CommandManager.CmdOpenDonatePage(); };

			ReportBug.Text = "Suggest a Feature";
			ReportBug.Click += delegate { CommandManager.CmdOpenSuggestPage(); };

			AddFeed.Text = "Add New Feed";
			AddFeed.Click += AddFeed_Click;

			DeleteFeed.Text = "Delete Feed";
			WellKnownMenus[WellKnownMenuItems.Feed_ManageFeeds_DeleteFeed] = DeleteFeed;
			DeleteFeed.Click += DeleteFeed_Click;

			ImportOPML.Text = "Import Feeds";
			ImportOPML.Click += ImportOPML_Click;

			ExportOPML.Text = "Export Feeds";
			ExportOPML.Click += ExportOPML_Click;

			ShowCategories.Text = "Categories";
			ShowCategories.Click += Categories_Click;

			ClearCache.Text = "Clear Feed Cache";
			ClearCache.Click += delegate { CommandManager.CmdClearFeedCache(); };

			CheckForUpdates.Text = "Check for Updates";
			CheckForUpdates.Click += delegate { CommandManager.CmdCheckForUpdates(); };

			ManageFeeds.Text = "Manage Feeds";
			WellKnownMenus[WellKnownMenuItems.Feed_ManageFeeds] = ManageFeeds;

			StopUpdate.Text = "Cancel Update";
			StopUpdate.Click += StopUpdate_Click;
			WellKnownMenus[WellKnownMenuItems.Feed_UpdateStop] = StopUpdate;

			StartUpdate.Text = "Update and Download";
			WellKnownMenus[WellKnownMenuItems.Feed_UpdateStart] = StartUpdate;

			UpdateAll.Text = "All Feeds";
			UpdateAll.Click += UpdateAll_Click;

			UpdateCategory.Text = "Current Category";
			UpdateCategory.Click += UpdateCategory_Click;

			UpdateFeed.Text = "Selected Feed";
			UpdateFeed.Click += UpdateFeed_Click;

			UpdateFailedFeeds.Text = "Failed Feeds";
			UpdateFailedFeeds.Click += delegate { CommandManager.CmdStartBatchUpdateForFailedFeeds(); };

			ShowUpdateReport.Text = "View Update Log";
			ShowUpdateReport.Click += delegate { CommandManager.CmdShowUpdateReport(); };
			ShowHelp.Text = "How Do I...";
			ShowHelp.Click += delegate { CommandManager.CmdShowHelp(); };


			var ViewPlayer = new MenuItem {Text = "Player"};
			WellKnownMenus[WellKnownMenuItems.Feed_ViewPlayer] = ViewPlayer;
			ViewPlayer.Click += delegate { CommandManager.CmdGoToView(Helpers.ViewNames.Player); };


			Help.Text = "Help";

			Help.MenuItems.Add(ShowHelp);
			Help.MenuItems.Add(CheckForUpdates);
			Help.MenuItems.Add(Contribute);
			Help.MenuItems.Add(ReportBug);
			Help.MenuItems.Add(About);

			StartUpdate.MenuItems.Add(UpdateCategory);
			StartUpdate.MenuItems.Add(UpdateFeed);
			StartUpdate.MenuItems.Add(UpdateAll);
			StartUpdate.MenuItems.Add(UpdateFailedFeeds);
			StartUpdate.MenuItems.Add(ShowUpdateReport);
			StartUpdate.Popup += delegate
			                     	{
			                     		UpdateFeed.Enabled = ViewManager.Feeds.SelectedFeed != null &&
			                     		                     ViewManager.Feeds.SelectedFeed.HasUrl;

			                     		ShowUpdateReport.Enabled = File.Exists(Configuration.FeedUpdateLogFilePath);
			                     	};

			m_FeedsMenu = new MainMenu();

			// 
			// MainMenu
			// 
			m_FeedsMenu.MenuItems.Add(OpenSelectedFeed);
			m_FeedsMenu.MenuItems.Add(Menu);
			ManageFeeds.MenuItems.Add(AddFeed);
			ManageFeeds.MenuItems.Add(DeleteFeed);
			ManageFeeds.MenuItems.Add(FeedProperties);
			ManageFeeds.MenuItems.Add(ImportOPML);
			ManageFeeds.MenuItems.Add(ExportOPML);
			// 
			// RMenu
			// 

			Tools.MenuItems.Add(Settings);
			Tools.MenuItems.Add(DeleteOldTracks);
			Tools.MenuItems.Add(DeleteAllPlayed);
			Tools.MenuItems.Add(ClearCache);
			Tools.MenuItems.Add(Refresh);


			Menu.MenuItems.Add(ManageFeeds); //needs at least one item
			Menu.Popup += OnFeedsRightMenuPopup;
			Menu.Text = "Menu";

			m_FeedsRightMenus.Add(WhatIsNew);
			m_FeedsRightMenus.Add(ManageFeeds);
			m_FeedsRightMenus.Add(ShowCategories);
			m_FeedsRightMenus.Add(ViewPlayer);
			m_FeedsRightMenus.Add(Tools);
			m_FeedsRightMenus.Add(Help);
			m_FeedsRightMenus.Add(ExitApplication);
		}

		/// <summary>
		/// Builds the feeds menu.
		/// </summary>
		/// <returns></returns>
		private static void BuildFeedContentMenu()
		{
			var GoBack = new MenuItem();
			WellKnownMenus[WellKnownMenuItems.FeedContent_GoBack] = GoBack;
			GoBack.Text = "Feeds";
			GoBack.Click += delegate
			                	{
			                		CommandManager.CmdFeedContentBrowserStop();
			                		CommandManager.CmdGoBack();
			                	};

			var UpdateFeed = new MenuItem {Text = "Update Feed"};
			UpdateFeed.Click += FeedRefresh_Click;
			WellKnownMenus[WellKnownMenuItems.FeedContent_Update] = UpdateFeed;

			var RefreshPage = new MenuItem {Text = "Refresh"};
			RefreshPage.Click += delegate { CommandManager.CmdRefreshFeedContent(); };

			var Stop = new MenuItem {Text = "Stop"};
			Stop.Click += delegate { ViewManager.FeedContent.BrowserStop(); };

			var Podcasts = new MenuItem {Text = "Feed Podcasts"};
			Podcasts.Click +=
				delegate { CommandManager.CmdToggleFeedView(ViewManager.FeedContent.CurrentFeed, FeedViewType.Tracks); };

			var ViewPlayer = new MenuItem {Text = "Player"};
			ViewPlayer.Click += delegate { CommandManager.CmdGoToView(Helpers.ViewNames.Player); };

			var ViewNextFeed = new MenuItem {Text = "Next Unread Feed"};
			ViewNextFeed.Click += delegate { CommandManager.CmdShowNextFeedContent(1, true); };

			var MarkAllAsRead = new MenuItem {Text = "Mark All as Read"};
			MarkAllAsRead.Click += delegate { CommandManager.CmdMarkAllFeedItemsAsRead(ViewManager.FeedContent.CurrentRssFeed); };

			var Exit = new MenuItem {Text = "Exit"};
			Exit.Click += delegate { CommandManager.CmdExitApplication(); };


			var Speak = new MenuItem { Text = "Speak Content" };
			Speak.Click += delegate { ViewManager.FeedContent.SpeakCurrentDocument(); };

			var Menu = new MenuItem {Text = "Menu"};
			Menu.Popup += delegate
			              	{
			              		RefreshPage.Enabled =
			              			/*ViewManager.FeedContent.CurrentFeed == null || (ViewManager.FeedContent.IsShowingWebPage &&*/
			              			!ViewManager.FeedContent.IsUpdating;
			              		Stop.Enabled = ViewManager.FeedContent.IsBrowserBusy &&
			              		               !ViewManager.FeedContent.IsUpdating;

			              		Podcasts.Enabled = ViewManager.FeedContent.CurrentFeed != null &&
			              		                   ViewManager.FeedContent.CurrentFeed.Tracks.Count > 0;

			              		Feed f = ViewManager.Feeds.GetNextUnreadFeed();
			              		ViewNextFeed.Enabled = ViewManager.FeedContent.CurrentFeed != null &&
			              		                       !ViewManager.FeedContent.CurrentFeed.IsAggregate &&
			              		                       f != null && f.HasUrl;

			              		MarkAllAsRead.Enabled = ViewManager.FeedContent.CurrentFeed != null &&
			              		                        ViewManager.FeedContent.CurrentRssFeed != null &&
			              		                        ViewManager.FeedContent.CurrentRssFeed.AreAllItemsRead() == false;

			              		if (ViewManager.FeedContent.IsUpdating)
			              		{
			              			UpdateFeed.Text = "Stop Update";
			              			UpdateFeed.Enabled = ViewManager.FeedContent.CurrentFeed != null &&
			              			                     !ViewManager.FeedContent.CurrentFeed.IsAggregate;
			              		}
			              		else
			              		{
			              			UpdateFeed.Text = "Update Feed";
			              			UpdateFeed.Enabled = ViewManager.FeedContent.CurrentFeed != null &&
			              			                     !ViewManager.FeedContent.IsShowingWebPage &&
			              			                     !ViewManager.FeedContent.CurrentFeed.IsAggregate;
			              		}


			              		ViewPlayer.Enabled = ViewManager.MediaPlayer.HasPlaylist;
			              	};

			Menu.MenuItems.Add(ViewNextFeed);
			Menu.MenuItems.Add(MarkAllAsRead);
			Menu.MenuItems.Add(UpdateFeed);
			Menu.MenuItems.Add(Stop);
			Menu.MenuItems.Add(RefreshPage);
			Menu.MenuItems.Add(ViewPlayer);
			Menu.MenuItems.Add(Podcasts);

			if(!String.IsNullOrEmpty(Configuration.TTSHandler))
				Menu.MenuItems.Add(Speak);

			Menu.MenuItems.Add(Exit);

			m_FeedContentMenu = new MainMenu();

			// 
			// MainMenu
			// 
			m_FeedContentMenu.MenuItems.Add(GoBack);
			m_FeedContentMenu.MenuItems.Add(Menu);
		}

		/// <summary>
		/// Builds the feeds menu.
		/// </summary>
		/// <returns></returns>
		private static void BuildBrowserMenu()
		{
			var GoBack = new MenuItem {Text = "Go Back"};
			GoBack.Click += delegate
			                	{
			                		CommandManager.CmdWebBrowserStop();
			                		CommandManager.CmdGoBack();
			                	};

			var RefreshPage = new MenuItem {Text = "Refresh"};
			RefreshPage.Click += delegate { ViewManager.WebBrowser.BrowserRefresh(); };

			var Stop = new MenuItem {Text = "Stop"};
			Stop.Click += delegate { ViewManager.WebBrowser.BrowserStop(); };

			var ViewContent = new MenuItem {Text = "Back to Feed"};
			ViewContent.Click += delegate
			                     	{
			                     		CommandManager.CmdWebBrowserStop();
			                     		CommandManager.CmdGoToView(Helpers.ViewNames.FeedContent);
			                     	};

			var Exit = new MenuItem {Text = "Exit"};
			Exit.Click += delegate { CommandManager.CmdExitApplication(); };

			var Menu = new MenuItem {Text = "Menu"};
			Menu.Popup += delegate { Stop.Enabled = ViewManager.WebBrowser.IsBrowserBusy; };

			Menu.MenuItems.Add(Stop);
			Menu.MenuItems.Add(RefreshPage);
			Menu.MenuItems.Add(ViewContent);
			Menu.MenuItems.Add(Exit);

			m_BrowserMenu = new MainMenu();

			// 
			// MainMenu
			// 
			m_BrowserMenu.MenuItems.Add(GoBack);
			m_BrowserMenu.MenuItems.Add(Menu);
		}

		/// <summary>
		/// Builds Categories Menu
		/// </summary>
		private static void BuildCategoriesMenu()
		{
			m_CategoriesMenu = new MainMenu();

			var close = new MenuItem
			            	{
			            		Text = "Back"
			            	};

			var manage = new MenuItem
			{
				Text = "Manage"
			};

			close.Click += delegate { CommandManager.CmdGoToView(Helpers.ViewNames.Feeds); };
			manage.Click += delegate
			{
				Categories.ShowCategoryEditor();
				ViewManager.Categories.OnBeforeActivate();
			};

			m_CategoriesMenu.MenuItems.Add(close);
			m_CategoriesMenu.MenuItems.Add(manage);
		}

		private static void Categories_Click(object sender, EventArgs e)
		{
			CommandManager.CmdGoToView(Helpers.ViewNames.Categories);
		}

		private static void ImportOPML_Click(object sender, EventArgs e)
		{
			CommandManager.CmdImportFeeds(null);
		}

		private static void ExportOPML_Click(object sender, EventArgs e)
		{
			CommandManager.CmdExportFeeds();
		}

		#region Menu Event Handlers

		private static void UpdateFeed_Click(object sender, EventArgs e)
		{
			if (ViewManager.Feeds.SelectedFeed != null)
				CommandManager.CmdStartBatchUpdate(ViewManager.Feeds.SelectedFeed);
		}

		private static void UpdateCategory_Click(object sender, EventArgs e)
		{
			CommandManager.CmdStartBatchUpdate(Configuration.ActiveFeedCategory);
		}

		private static void UpdateAll_Click(object sender, EventArgs e)
		{
			CommandManager.CmdStartBatchUpdate(CategoryManager.AllFeeds);
		}

		private static void StopUpdate_Click(object sender, EventArgs e)
		{
			CommandManager.CmdTerminateBatchUpdate();
		}

		private static void OnFeedsRightMenuPopup(object sender, EventArgs e)
		{
			var m = sender as MenuItem;

			if (m.MenuItems.Contains(WellKnownMenus[WellKnownMenuItems.Feed_UpdateStop]))
				m.MenuItems.Remove(WellKnownMenus[WellKnownMenuItems.Feed_UpdateStop]);

			if (m.MenuItems.Contains(WellKnownMenus[WellKnownMenuItems.Feed_UpdateStart]))
				m.MenuItems.Remove(WellKnownMenus[WellKnownMenuItems.Feed_UpdateStart]);

			if (RssFeedUpdateManager.IsUpdating || EnclosureDownloadManager.IsDownloading)
			{
				m.MenuItems.Add(WellKnownMenus[WellKnownMenuItems.Feed_UpdateStop]);

				WellKnownMenus[WellKnownMenuItems.Feed_UpdateStop].Text = EnclosureDownloadManager.IsDownloading ? "Stop Download" : "Stop Update";
			}
			else
			{
				m.MenuItems.Add(WellKnownMenus[WellKnownMenuItems.Feed_UpdateStart]);
			}

			WellKnownMenus[WellKnownMenuItems.Feed_ManageFeeds_EditFeed].Enabled =
				WellKnownMenus[WellKnownMenuItems.Feed_ManageFeeds_DeleteFeed].Enabled =
				ViewManager.Feeds.SelectedFeed != null && !ViewManager.Feeds.SelectedFeed.IsSubFeed;

			foreach (MenuItem menu in m_FeedsRightMenus)
			{
				if (m.MenuItems.Contains(menu))
					m.MenuItems.Remove(menu);

				m.MenuItems.Add(menu);
			}

			WellKnownMenus[WellKnownMenuItems.Feed_ViewPlayer].Enabled = ViewManager.MediaPlayer.HasPlaylist;
		}


		private static void StartStopDownload_Click(object sender, EventArgs e)
		{
			CommandManager.CmdPauseResumeDownload();
		}

		private static void DeleteOldTracks_Click(object sender, EventArgs e)
		{
			CommandManager.CmdDeleteOldFeeds();
		}

		private static void FeedRefresh_Click(object sender, EventArgs e)
		{
			CommandManager.CmdUpdateFeed();
		}

		private static void PrevTrack_Click(object sender, EventArgs e)
		{
			CommandManager.CmdPlayPreviousTrack();
		}

		private static void PlayNext_Clik(object sender, EventArgs e)
		{
			CommandManager.CmdPlayNextTrack();
		}

		private static void PlayPause_Click(object sender, EventArgs e)
		{
			CommandManager.CmdPlayPause();
		}

		private static void PlayerMapKeys_Click(object sender, EventArgs e)
		{
			CommandManager.CmdShowShortcutEditorForm();
		}

		private static void FeedProperties_Click(object sender, EventArgs e)
		{
			CommandManager.CmdShowFeedPropertiesDialog();
		}

		private static void ViewFeedContent_Click(object sender, EventArgs e)
		{
			CommandManager.CmdOpenFeedUrl();
		}

		private static void AddFeed_Click(object sender, EventArgs e)
		{
			CommandManager.CmdAddFeed();
		}

		private static void DeleteFeed_Click(object sender, EventArgs e)
		{
			CommandManager.CmdDeleteFeed();
		}

		private static void Refresh_Click(object sender, EventArgs e)
		{
			CommandManager.CmdRefreshFeeds();
		}

		private static void PlayerViewSmall_Click(object sender, EventArgs e)
		{
			foreach (MenuItem m in ((MenuItem) sender).Parent.MenuItems)
				m.Checked = false;
			((MenuItem) sender).Checked = true;
			CommandManager.CmdSwitchPlayerView(CoreHelper.PlayerView.Small);
		}

		private static void PlayerViewNormal_Click(object sender, EventArgs e)
		{
			foreach (MenuItem m in ((MenuItem) sender).Parent.MenuItems)
				m.Checked = false;
			((MenuItem) sender).Checked = true;
			CommandManager.CmdSwitchPlayerView(CoreHelper.PlayerView.Normal);
		}

		private static void TogglePlayed_Click(object sender, EventArgs e)
		{
			CommandManager.CmdToggleTrackPlayedStatus();
		}

		private static void ToggleViewAll_Click(object sender, EventArgs e)
		{
			CommandManager.CmdToggleTrackViewAll();
		}

		private static void DeletePlayedInFeed_Click(object sender, EventArgs e)
		{
			CommandManager.CmdDeletePlayedInFeed();
		}

		private static void GoBack_Click(object sender, EventArgs e)
		{
			CommandManager.CmdGoToView(Helpers.ViewNames.Feeds);
		}

		private static void Exit_Click(object sender, EventArgs e)
		{
			CommandManager.CmdExitApplication();
		}

		private static void DeleteAllPlayed_Click(object sender, EventArgs e)
		{
			CommandManager.CmdDeleteAllPlayedTracks();
		}

		private static void Open_Click(object sender, EventArgs e)
		{
			CommandManager.CmdViewSelectedFeedPodcasts();
		}

		private static void DeleteSelected_Click(object sender, EventArgs e)
		{
			CommandManager.DeleteSelectedTrack();
		}

		private static void About_Click(object sender, EventArgs e)
		{
			CommandManager.CmdShowAboutScreen();
		}

		#endregion
	}
}